﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using SdmxMl.Common;

namespace SdmxMl.UiEditors
{
    public partial class UcMultiLang : UserControl
    {
        public IWindowsFormsEditorService _wfes;

        private MultiLang ml;
        private int[] arrColLoc;
        DataTable tab = null;

        public int[] ArrLoc
        {
            get { return arrColLoc; }
        }

        public UcMultiLang(ref int[] arrCol)
        {
            Init();
            arrColLoc = arrCol;
            Array.Resize(ref arrColLoc, LangSupport.Count);
            int i = 0;
            foreach(string lg in LangSupport.SupportedLanguages)
                tab.Rows.Add(new object[] { lg, arrColLoc[i++].ToString() });
            this.dataGridView1.DataSource = tab;

            this.dataGridView1.Columns[1].HeaderText = "Column position";
            this.dataGridView1.Rows[0].Cells[1].Selected = true;
            this.dataGridView1.Rows[0].Cells[1].Selected = true;
        }

        public UcMultiLang(MultiLang ml)
        {
            Init();
            this.ml = ml;

            foreach (string l in LangSupport.SupportedLanguages)
                tab.Rows.Add(new object[] { l, ml.GetForLanguage(l) });

            this.dataGridView1.DataSource = tab;
        }

        private void Init()
        {
            InitializeComponent();
            tab = new DataTable();
            tab.Columns.Add(new DataColumn("Lang"));
            tab.Columns.Add(new DataColumn("Column"));

            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
 
        }


        private void butCancel_Click(object sender, EventArgs e)
        {
  //          _wfes.CloseDropDown();
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            dataGridView1.EndEdit();

            if (ml != null)
            {
                ml.TextList.Clear();
                foreach (DataRow r in tab.Rows)
                {
                    if (r[1].ToString().Trim().Length > 0)
                        ml.TextList.Add(new TextType(r[0].ToString(), r[1].ToString().Trim()));
                }
            }
            else if (arrColLoc != null)
            {
                
                for (int lg = 0; lg < LangSupport.Count && lg < tab.Rows.Count; lg++ )
                    arrColLoc[lg] = Convert.ToInt32(tab.Rows[lg][1]);
            }

  //          _wfes.CloseDropDown();

        }

        private void UcMultiLang_Leave(object sender, EventArgs e)
        {
            butSave_Click(sender, e);
        }
    }
}
